var app = getApp();
var baseUrl = app.globalData.host;
Component({
  data: {
    showModal:false,
    batchLeftIds:'',
    batchRightIds:'',
    list: [
      {
        name:'泡发(4寸)',
        id: '202417'
      },
      {
        name:'削发(4寸)',
        id: '202413'
      },
      {
        name:'削发(6寸)',
        id: '202414'
      },
      {
        name:'泡发(6寸)',
        id: '202418'
      },
      {
        name:'泡发(8寸)',
        id: '202419'
      },
      {
        name:'削发(8寸)',
        id: '202415'
      },
      {
        name:'泡发(12寸)',
        id: '202420'
      },
      {
        name:'削发(12寸)',
        id: '202416'
      },
      {
        name:'教习头(8寸)',
        id: '202421'
      },
      {
        name:'教习头(12寸)',
        id: '202422'
      },
      {
        name:'教习头(16寸)',
        id: '202423'
      },
      {
        name:'教习头(18寸)',
        id: '202424'
      }
    ],
    lists:[
      {
        name:'模具',
        id: '202402'
      },
      {
        name:'手套',
        id: '202407'
      },
      {
        name:'木材',
        id: '202401'
      },
      {
        name:'螺杆',
        id: '202425'
      },
      {
        name:'集成材',
        id: '202412'
      },
      {
        name:'汽摩配',
        id: '202403'
      },
      {
        name:'柿饼加工',
        id: '202426'
      },
      {
        name:'电瓷行业',
        id: '202411'
      },
      {
        name:'纸制品加工',
        id: '202410'
      },
      {
        name:'茶叶加工行业',
        id: '202427'
      },
      {
        name:'不锈钢加工行业',
        id: '202408'
      },
      {
        name:'户用光伏发电站',
        id: '202409'
      },
      {
        name:'木材单板加工行业',
        id: '202404'
      },
      {
        name:'水暖铸造抛光行业',
        id: '202405'
      },
      {
        name:'水暖组装加工行业',
        id: '202406'
      }
    ],
    batchName:'请选择加工种类',
    licenses: ["请选择","新客户", "老客户"],
    licenseIndex: 0,
    formData: {
      batchIds:''
    },
  },
  methods: {
    // 外面的弹窗
    btn:function () {
      var batchIds = this.data.formData.batchIds;
      var list = this.data.list;
      var lists = this.data.lists;
      console.info(batchIds);
      for (let i = 0; i < list.length; i++) {
        if(batchIds.length<=0)
        {
          this.data.list[i].checked = false
        }
        else
        {
          for (let index = 0; index < batchIds.length; index++) {
            if(parseInt(batchIds[index]) === parseInt(list[i].id))
            {
              this.data.list[i].checked = true
              break;
            }
            else{
              this.data.list[i].checked = false
            }
          }
        }
       
      }
    for (let j = 0; j < lists.length; j++) {
      if(batchIds.length<=0)
      {
        this.data.lists[j].checked = false
      }
      else
      {
        for (let index = 0; index < batchIds.length; index++) {
          if(parseInt(batchIds[index]) === parseInt(lists[j].id))
          {
            this.data.lists[j].checked = true
            break;
          }
          else{
            this.data.lists[j].checked = false
          }
        }
      }
    }  
      console.info(list);
      console.info(lists);
      this.setData({
        showModal:true,
        list:list,
        lists:lists
      })
    },
    // 禁止屏幕滚动
    preventTouchMove:function () {
    },
    // 弹出层里面的弹窗
    ok:function () {
      let name = '';
      let left = this.data.batchLeftIds;
      let right = this.data.batchRightIds;
      var list = this.data.list;
      var lists = this.data.lists;
      for (let index = 0; index < left.length; index++) {
        for (let i = 0; i < list.length; i++) {
          if(left[index] == list[i].id)
          {
            name +=list[i].name+",";
            break;
          }
        }
      }
      for (let index = 0; index < right.length; index++) {
        for (let i = 0; i < lists.length; i++) {
          if(right[index] == lists[i].id)
          {
            name +=lists[i].name+",";
            break;
          }
        }
      }
      name = name.substring(0, name.lastIndexOf(','));
      let ids = [];
      if(this.data.batchLeftIds.length>0)
      {
       ids = this.data.batchLeftIds.concat(this.data.batchRightIds)
      }
      else if(this.data.batchRightIds.length>0)
      {
        ids = this.data.batchRightIds.concat(this.data.batchLeftIds)
      }
      this.setData({
        showModal:false,
        [`formData.batchIds`]: ids,
        batchName:name
      })
    },
    no:function(){
      this.setData({
        showModal:false
      })
    },
    checkboxLeftChange:function(e){
      this.setData({
        batchLeftIds: e.detail.value,//单个选中的值
      })
      console.info(this.data.batchLeftIds);
    },
    checkboxRightChange:function(e){
      this.setData({
        batchRightIds:e.detail.value,
      })
      console.info(this.data.batchRightIds);
    },
    bindBusinessLicense: function(e) {
      console.log('picker country 发生选择改变，携带值为', e.detail.value);
  
      this.setData({
          licenseIndex: e.detail.value,
          [`formData.licenseIndex`]: e.detail.value
      })
    },

    submitForm: function() {
      var data = this.data.formData;
      console.info(data);
      this.setData({
          disabled:true
      });
      var obj = {
        data: this.data.formData
      }
      wx.request({
        url: baseUrl+"/report/processTrade",
        method: 'POST',
        data: obj,
        header: {
          'content-type': 'application/json'
        },
        dataType: 'json',
        success: function(res) { 
          wx.showToast({
            title: "保存成功",
            icon: 'none',
            duration: 2000,
            success:function(){
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({ //跳转到前一个页面
                  delta:1,//前一个页面
                });
              }, 2000) //延迟时间
            }
          });
        },
        fail: function(res) {
          wx.showToast({
            title: '服务器网络错误,请稍后重试',
            icon: 'loading',
            duration: 1500
          })
        },
        complete: function(res) {
        },
      })
    },

    onLoad: function(option) {
      var that = this;
      that.setData({
        custId:option.custId,
          [`formData.custId`]: option.custId
      });
      var data = that.data.formData;
      //查询数据回显
    //   wx.request({
    //     url: baseUrl+"/report/selectBreed",
    //     method: 'POST',
    //     data: data,
    //     header: {
    //         'content-type': 'application/json'
    //     },
    //     dataType: 'json',
    //     success: function(res) { 
    //         console.info(res.data.code);
    //         if(res.data.code == 2)
    //         {
    //           wx.showToast({
    //             title: res.data.data,
    //             icon: 'none',
    //             duration: 2000,
    //             success:function(){
    //               setTimeout(function () {
    //                 //要延时执行的代码
    //                 wx.navigateBack({ //跳转到前一个页面
    //                   delta:1,//前一个页面
    //                 });
    //               }, 2000) //延迟时间
    //             }
    //           });
    //         }
    //         else
    //         {
    //           let lists = that.data.lists;
    //           if(res.data.data.length>0){
    //             lists.pop();  //清空
    //             let newData = {};
    //             for (let i = 0; i < res.data.data.length; i++) {
    //               lists.push(newData);//放入相同量的数组
    //             }
    //             that.setData({
    //               lists: lists,
    //               formData: res.data.data,
    //             })
    //           }
    //         }
    //     },
    //     fail: function(res) {
    //         wx.showToast({
    //         title: '服务器网络错误,请稍后重试',
    //         icon: 'loading',
    //         duration: 1500
    //         })
    //     },
    //     complete: function(res) {
    //     },
    // })
    // 
  }
  }
});